package ua.epam.java_bravo.controller;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import ua.epam.java_bravo.controller.servletCommand.ICommand;
import ua.epam.java_bravo.manager.ConfigurationManager;
import ua.epam.java_bravo.manager.MessageManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Main servlet
 *
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 15.12.2010
 * Time: 13:21:18
 */
public class Controller extends HttpServlet{
    private static final Logger log =
            Logger.getLogger(Controller.class.getName());

    //Object that contains list of available commands
    RequestHelper requestHelper = null;


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        BasicConfigurator.configure();
        log.info("initialized");
        requestHelper = RequestHelper.getInstance();
    }

    @Override
    public void destroy() {
        super.destroy();
        log.info("destroyed");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info(Controller.class.getName() + " doGet() started");
        doPost(req, resp);
        log.info(Controller.class.getName() + " doGet() complete");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        log.info(Controller.class.getName() + " doPost() started");
        processRequest(req, resp);
        log.info(Controller.class.getName() + " doPost() started");

    }


    /**
     * Executes request (command) and redirecting to response .jspx page
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void processRequest(HttpServletRequest request,
                                HttpServletResponse response) throws ServletException, IOException {
       String page = null;
        try {
            //receiving command from .jspx page
            ICommand command = requestHelper.getCommand(request);
            log.info(Controller.class.getName() + " command is: " + command.toString());

            //passing execution of request to proper ICommand
            //and redirecting to the resulting .jspx
            page = command.execute(request, response);
            log.info(Controller.class.getName() + " page is: " + page);

        } catch (ServletException e) {
            e.printStackTrace();
            //generating error message
            request.setAttribute("errorMessage", MessageManager.getInstance().getProperty(
                    MessageManager.SERVLET_EXCEPTION_ERROR_MESSAGE
            ));

            page = ConfigurationManager.getInstance().
                    getProperty(ConfigurationManager.ERROR_PAGE_PATH);
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", MessageManager.getInstance().getProperty(
                    MessageManager.IO_EXCEPTION_ERROR_MESSAGE
            ));

            page = ConfigurationManager.getInstance().
                    getProperty(ConfigurationManager.ERROR_PAGE_PATH);
        }

        //redirecting to page-response
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(page);
        dispatcher.forward(request, response);
    }


}
